﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SystemManagement
{
    public partial class RoleCommision : BasePage
    {
        private IModulesBLL modulesBLL = IocContainer.Instance.Resolve<IModulesBLL>();
        private IRoleBLL roleBll = IocContainer.Instance.Resolve<IRoleBLL>();
        private DataTable dtFunction = IocContainer.Instance.Resolve<IFunctionBLL>().GetAllFunction();

        protected int roleId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            int.TryParse(Request.QueryString["RoleID"], out roleId);

            if(!IsPostBack)
            {
                BindModules();
                
                BindRoleInfo(roleId);

                int moduleId = 0;
                int.TryParse(this.ddlModules.SelectedValue, out moduleId);
                BindPages(roleId, moduleId);
            }

        }

        protected  void BindRoleInfo(int roleId)
        {
            Skyworth.CRM.Service.Entity.Roles roles = roleBll.GetRole(roleId);
            this.labRoleName.Text = roles.RoleName;
        }

        protected  void BindModules()
        {
            DataTable dataTable = modulesBLL.GetModules();
            //this.ddlModules.DataTextField = "ModuleName";
            //this.ddlModules.DataValueField = "ModuleID";
            this.ddlModules.DataSource = dataTable;
            this.ddlModules.DataBind();
        }

        protected  void BindPages(int roleId,int moduleId)
        {
            DataTable dtModules = modulesBLL.GetPagesWithFunctionAndAuthorityByRole(moduleId, roleId);

            this.gvModuleView.DataSource = dtModules;
            this.gvModuleView.DataBind();
        }

        protected void ModuleView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBoxList AuthorityList = (CheckBoxList)e.Row.FindControl("AuthorityList");
                Label menu_ID = (Label)e.Row.FindControl("lab_ID");
                Label lab_Verify = (Label)e.Row.FindControl("lab_Verify");

               Boolean isEnabledLink = false;
                int n = dtFunction.Rows.Count;//系统功能个数
                //获取系统配置的权限列表，如果模块没有该权限，则禁用该权限
                for (int i = 0; i < n; i++)
                {
                    string functionID = dtFunction.Rows[i]["FunctionID"].ToString();
                    string functionName = dtFunction.Rows[i]["FunctionName"].ToString();
                    AuthorityList.Items.Add(new ListItem(functionName, functionID));
                    AuthorityList.Items[i].Enabled = false;

                    string pageFunction=this.gvModuleView.DataKeys[e.Row.RowIndex]["pageFunction"].ToString();
                    if(pageFunction.Contains(","+functionID+"-1"))
                    {
                            AuthorityList.Items[i].Enabled = true;
                           isEnabledLink = true;// 只要有一个功能可用，更新连接就可用
                    }

                    string pageAuthority = this.gvModuleView.DataKeys[e.Row.RowIndex]["pageAuthority"].ToString();
                    AuthorityList.Items[i].Selected = pageAuthority.Contains(","+functionID+"-1");
                }
                if (isEnabledLink)
                {
                    LinkButton btnUpdate = (LinkButton) e.Row.FindControl("btn_update");
                    btnUpdate.Enabled = isEnabledLink;
                }

            }
        }

        protected void ModuleView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            try
            {

                CheckBoxList cal = (CheckBoxList)this.gvModuleView.Rows[e.NewSelectedIndex].Cells[1].FindControl("AuthorityList");

                Dictionary<string, int> dtFunctionId = new Dictionary<string, int>();

                for (int i = 0; i < cal.Items.Count; i++)
                {
                    ListItem functionItem = cal.Items[i];
                    if (functionItem.Enabled)
                    {
                        dtFunctionId.Add(functionItem.Value, functionItem.Selected ? 1 : 0);
                    }
                }
                Label labelMenu = (Label)this.gvModuleView.Rows[e.NewSelectedIndex].Cells[0].FindControl("lab_ID");
                int pageId = 0;
                int.TryParse(labelMenu.Text, out pageId);
                roleBll.SetRoleAuthority(roleId, pageId, dtFunctionId);

                Alert("权限设置完成！");

            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }

        }

        protected void ddlModules_SelectedIndexChanged(object sender, EventArgs e)
        {
            int moduleId = 0;
            int.TryParse(this.ddlModules.SelectedValue, out moduleId);
            BindPages(roleId, moduleId);
        }
    }
}
